/**
 * Jquery Array Function Json Tpl 扩展 * 
 */
Array.prototype.indexOf = function(o){var l = this.length;for (var i = 0; i < l; i++){if (this[i] == o){return i;}}return -1;};
Array.prototype.remove  = function(n){this.splice(n,1);};
Array.prototype.replace = function(n,vars){this.splice(n,1,vars);};
String.prototype.trim   = function(){return this.replace(/^\s+|\s+$/g, "");}
Function.prototype.defer= function(C,A){var B=this;if(A){window.setTimeout(function(){B.call(A);},C);}else {window.setTimeout(this,C);}};
if(window.addEventListener)
{ 
    function __element_style()
	{ 
	    return(this.style); 
	} 
	function __window_event()
	{ 
	    return(__window_event_constructor()); 
	} 
	function __event_srcElement()
	{ 
	    return this.target; 
	}
	function __window_event_constructor()
	{ 
	    if(document.all) return(window.event);
	    
	    var _caller=__window_event_constructor.caller; 
	    while(_caller!=null)
	    {
	        var _argument=_caller.arguments[0]; 
	        if(_argument)
	        { 
	            var _temp=_argument.constructor;
	            if(_temp.toString().indexOf("Event")!=-1)
	                return(_argument); 
	        }
	        _caller=_caller.caller; 
	    }
	    return(null); 
	}
    function __firefox()
    {
	    HTMLElement.prototype.__defineGetter__("runtimeStyle", __element_style);
	    window.constructor.prototype.__defineGetter__("event", __window_event); 
	    Event.prototype.__defineGetter__("srcElement", __event_srcElement); 
	}	
    __firefox(); 
}

jQuery.cookie = function(name, value, options)
{
	if (typeof value != 'undefined') 
	{ // name and value given, set cookie
		options = options || {expires:1,path:"\/"};
		var expires = '';
		if (options.expires && (typeof options.expires == 'number' || options.expires.toGMTString)) 
		{
			var date;
			if (typeof options.expires == 'number') 
			{
				date = new Date();
				date.setTime(date.getTime() + (options.expires * 60 * 60 * 1000));
			} else 
			{
				date = options.expires;
			}
			expires = '; expires=' + date.toGMTString(); // use expires attribute, max-age is not supported by IE
		}
		var path = options.path ? '; path=' + options.path : '';
		var domain = options.domain ? '; domain=' + options.domain : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	} else 
	{ // only name given, get cookie
		var cookieValue = '';
		if (document.cookie && document.cookie != '') 
		{
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) 
			{
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) 
				{
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};
/**
 * var xml = $.loadXML("PluginConf.xml");
 * var obj = $(xml).toObject().get(0);
 * alert(obj.pool);
 */
jQuery.Xml2Object=function(/*Element*/elNode)
{
	/*NamedNodeMap*/
	var Obj=new Object;
	var nodeAttr=elNode.attributes;
	if(nodeAttr != null)
	{
		for(var i=0;i<nodeAttr.length;i++)
		{
			Obj[nodeAttr[i].name]=nodeAttr[i].value;
		}
	}
	var nodeText="text";
	if (elNode.text==null) nodeText="textContent";
	/*NodeList*/
	var nodeChilds=elNode.childNodes;
	if(nodeChilds!=null)
	{
		for(var i=0;i<nodeChilds.length;i++)
		{
			if (nodeChilds[i].tagName!=null)
			{
				if (nodeChilds[i].childNodes[0]!=null
						&& nodeChilds[i].childNodes.length<=1
						&& (nodeChilds[i].childNodes[0].nodeType==3 || nodeChilds[i].childNodes[0].nodeType==4)
					)
				{
					if (Obj[nodeChilds[i].tagName]==null)
					{
						Obj[nodeChilds[i].tagName]=nodeChilds[i][nodeText];
					}else
					{
						if (typeof(Obj[nodeChilds[i].tagName])=="object"&&Obj[nodeChilds[i].tagName].length)
						{
							Obj[nodeChilds[i].tagName][Obj[nodeChilds[i].tagName].length]=nodeChilds[i][nodeText];
						}else
						{
							Obj[nodeChilds[i].tagName]=[Obj[nodeChilds[i].tagName]];
							Obj[nodeChilds[i].tagName][1]=nodeChilds[i][nodeText];
						}
					}
				}else
				{
					if (nodeChilds[i].childNodes.length)
					{
						if (Obj[nodeChilds[i].tagName]==null)
						{
							Obj[nodeChilds[i].tagName]=new Object;
							buildObjectNode(Obj[nodeChilds[i].tagName],nodeChilds[i]);
						}else
						{
							if (Obj[nodeChilds[i].tagName].length)
							{
								Obj[nodeChilds[i].tagName][Obj[nodeChilds[i].tagName].length]=new Object;
								buildObjectNode(Obj[nodeChilds[i].tagName][Obj[nodeChilds[i].tagName].length-1],nodeChilds[i]);
							}else
							{
								Obj[nodeChilds[i].tagName]=[Obj[nodeChilds[i].tagName]];
								Obj[nodeChilds[i].tagName][1]=new Object;
								buildObjectNode(Obj[nodeChilds[i].tagName][1],nodeChilds[i]);
							}
						}
					}else
					{
						Obj[nodeChilds[i].tagName]=nodeChilds[i][nodeText];
					}
				}
			}
		}
	}
	return Obj;
};
jQuery.Object2Json = function(v)
{
	v = v || this.object || {};
	var m = 
	{
        '\b': '\\b',
        '\t': '\\t',
        '\n': '\\n',
        '\f': '\\f',
        '\r': '\\r',
        '"' : '\\"',
        '\\': '\\\\'
    },
    s = 
    {
        'array': function (x) 
        {
            var a = ['['], b, f, i, l = x.length, v;
            for (i = 0; i < l; i += 1) 
            {
                v = x[i];
                f = s[typeof v];
                if (f) {
                    v = f(v);
                    if (typeof v == 'string') 
                    {
                        if (b)
                        {
                            a[a.length] = ',';
                        }
                        a[a.length] = v;
                        b = true;
                    }
                }
            }
            a[a.length] = ']';
            return a.join('');
        },
        'boolean': function (x) 
        {
            return String(x);
        },
        'null': function (x) 
        {
            return "null";
        },
        'number': function (x) 
        {
            return isFinite(x) ? String(x) : 'null';
        },
        'object': function (x) 
        {
            if (x) 
            {
                if (x instanceof Array) 
                {
                    return s.array(x);
                }
                var a = ['{'], b, f, i, v;
                for (i in x) 
                {
                    v = x[i];
                    f = s[typeof v];
                    if (f) 
                    {
                        v = f(v);
                        if (typeof v == 'string') 
                        {
                            if (b) 
                            {
                                a[a.length] = ',';
                            }
                            a.push(s.string(i), ':', v);
                            b = true;
                        }
                    }
                }
                a[a.length] = '}';
                return a.join('');
            }
            return 'null';
        },
        'string': function (x) 
        {
            if (/["\\\x00-\x1f]/.test(x)) 
            {
                x = x.replace(/([\x00-\x1f\\"\\'])/g, function(a, b) 
                {
                    var c = m[b];
                    if (c) 
                    	return c;
                    c = b.charCodeAt();
                    return '\\u00' +
                        Math.floor(c / 16).toString(16) +
                        (c % 16).toString(16);
                });
            }
            return '"' + x + '"';
        }
    };
	var f = isNaN(v) ? s[typeof v] : s['number'];
	return f?f(v):false;
};
jQuery.LoadScript=function(url,callback,charset)
{
	if(callback)
	{
		if(/{callback}/.test(url))
		{
			url = url.replace( /{callback}/,callback);
		}else
		{
			url += (url.indexOf('?') == -1?'?':'&')+'callback='+callback;
		}		
	}
	charset = charset || '';
    if(jQuery.browser.msie && this.script)
    {
    	var script= this.script;
    	script.charset = charset;
		script.src = url;
		return;
    }else
    {
    	script = document.createElement( 'script' );
    	script.charset  = charset;
        script.type     = 'text/javascript';
        script.src 	    = url;
        document.getElementsByTagName('HEAD')[0].appendChild(script);
    }
    return script;
};
jQuery.StringEnCode = function (x) 
{
	if (/["\\\x00-\x1f]/.test(x)) 
	{
		var m = 
		{
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\'': '\\\'',
			'\\': '\\\\'
        }
		return x.replace(/([\x00-\x1f\\"\\'])/g, 
							function(a, b)
							{ 
								var c = m[b];
								if (c)return c;
								c = b.charCodeAt();
								return '\\u00' +Math.floor(c / 16).toString(16) +(c % 16).toString(16);
							});
	}
};
var Tpl = function(o,tpl)
{
	if(typeof o == "string")
	{
		this.object = document.getElementById(o);
	}else if(typeof o == "object")
	{
		this.object = o;
	}else
	{
		this.error = [1,"object error"];
	}
	if(this.object)
	{
		this.tpl = this.format(tpl || this.object.innerHTML);
	}
};
Tpl.prototype =
{
	init:function(data,tpl)
	{
		tpl = this.format(tpl) || this.tpl;
		//alert(tpl);
		tpl = this.parse(data,tpl,0);
		if(this.object)this.object.innerHTML = tpl;
		return tpl;
	},
	format:function(tpl)
	{
		return tpl?tpl.replace(/\[%/g,"<%").replace(/%\]/g,"%>")
				   .replace(/\{%/g,"<%").replace(/%\}/g,"%>")
				   .replace(/<!--%/g,"<%").replace(/%-->/g,"%>")
				   .replace(/\/\*%/g,"<%").replace(/%\*\//g,"%>"):false;
	},
	parse:function(data,tHtml,xLev)
	{
		//alert((new Json(data)).en());
		var xLev			= xLev || 0;
		var sRepeat			= "<%begin_"+xLev+"[^>]*%>((.|\\n)+?)<%end_"+xLev+"%>";
		var rKey_g			= new RegExp("<%begin_"+xLev+"\\s*key=\"([^\"]+)\"[^%]*%>","g");
		var rRepeat			= new RegExp(sRepeat);
		var rRepeat_g		= new RegExp(sRepeat,"g");
		var rVars			= new RegExp("<%=(.+?)%>","g");
		var tDate;
		// 是否有重复
		var aKey = tHtml.match(rKey_g);
		if(aKey)
		{
			var aRepeat = tHtml.match(rRepeat_g);
			for (var key=0;key<aKey.length;key++)
			{
				aKey[key] 		= aKey[key].replace(rKey_g,"$1");
				aRepeat[key] 	= aRepeat[key].replace(rRepeat,"$1");
				tDate			= data[aKey[key]];
				var html		= '';
				for(var key2 in tDate)
				{
					html += typeof tDate[key2] == "object" ? this.parse(tDate[key2],aRepeat[key],xLev+1) : "";
				}
				tHtml				= tHtml.replace(rRepeat,html);
			}
		}
		var aVars = tHtml.match(rVars);
		if(aVars)
		{
			for (key=0;key<aVars.length;key++)
			{
				aVars[key]		= aVars[key].replace(rVars,"$1");
				tDate 			= data[aVars[key]]?data[aVars[key]]:'';
				var rVars_		= new RegExp("<%="+aVars[key]+"%>");				
				tHtml			= tHtml.replace(rVars_,tDate);
			}
		}
		return tHtml;
	}
}
/**
 * urs
 */
var _ =  
{
	"go":function(url,t,note)
	{
		if(note)if(!window.confirm(note))return;
		if(t)
		{
			top.document.location = url;
		}else
		{
			document.location = url;
		}
	},
	"r":function(vars)
	{
		document.write(vars);
	},
	"e":function()
	{
		var elements = [];
		for (var i = 0; i < arguments.length; i++)
		{
			var element = arguments[i];
			if (typeof element == 'string')element = document.getElementById(element);
			if (arguments.length == 1)
				return element;elements.push(element);
		}
		return elements;
	},
	"get":function(name)
	{
		var get  = [location.search,location.hash].join('&');
		var start	=get.indexOf(name + '=');
		if (start == -1) return '';
		var len = start + name.length + 1;
		var end = get.indexOf('&',len);
		if (end == -1) end = get.length;
		return decodeURIComponent(get.substring(len,end));
	},
	"copy":function(vars,note)
	{
		if(note) alert(note);
		if(window.clipboardData) 
		{ 
             window.clipboardData.setData("Text", vars); 
			 return true;	
     	}else
		{
			return false;	
		}
	},
	"time":function(a,b)
    {
    	var s,d,y;	
    	d = new Date(a*1000);
		y = d.getYear();
		s = (jQuery.browser.msie?y:(y+1900)) + 
		"-"+ (d.getMonth() + 1) + 
		"-"+ d.getDate();
    	if(b != 1)
    	{
    		s += " "+ d.getHours() + 
	    		":"+ d.getMinutes() + 
	    		":"+ d.getSeconds();
    	}
    	return s;
    },
	"substr":function(s,n)	
	{
		s = s.toString();
		if(s)
		{
			n = n||20;
			if(s.length > n)
			{
				return s.substr(0,n)+'..';	
			}
		}
		return s;	
	},
	"size":function(a)
	{
		var b;
		if(a<1024)
		{
			b = 'Bit';
		}else if(a<1024*1024 && a>1024)
		{
			a = a/1024;
			b = 'K';
		}else if(a<1024*1024*1024 && a>1024*1024)
		{
			a = a/1024/1024;
			b = 'M';
		}else
		{
			a = a/1024/1024/1024;
			b = 'G';
		};
		return (Math.round(a*100)/100)+b;
	},
	"rand":function(begin,end)
	{
		if(typeof begin !='undefined')
		{
			end = end?end:2147483648;
			return Math.floor(Math.random()*(end-begin)+begin);
		}else
		{
			return (new Date()).getTime();
		}
	}
};
/**
 * URL相关函数
 */
var urlcode =
{
	en:function(o,url)
	{
		url = url || '';
		var rs  =[];
		for(var k in o) rs.push(k+'='+encodeURIComponent(o[k]));
		return url+(url.indexOf('?') == -1?'?':'&')+rs.join('&');	
	},
	de:function(str)
	{
		str = (str.indexOf('?') == -1?str:str.split("?")[1]).split("&");
		var rs = {},a,c   = str.length;
		for(var i=0;i<c;i++)
		{
			a = str[i].split("=");
			rs[a[0]] = decodeURIComponent(a[1]);
		}
		return rs;
	}
}
var Player = 
{
	"flash":function(e,v,p,w,h)
	{
		if(typeof e == "string") e = _.e(e);
		p = p || 'http://www.56.com/flashApp/v_player_site_fp7_5_20061219.swf';
		w = w || '100%';
		h = h || '100%';
		var wl = _.get('wl') || 0;
		e.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' +
						'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" ' +
						'width="'+w+'" ' +
						'height="'+h+'">' +
					'<param name="movie" value="'+(wl?p+'?'+v:p)+'">' +
					'<param name="quality" value="high">' +
					'<param name="allowScriptAccess" value="always" />' +
					'<param name="wmode" value="transparent" />' +
					'<param name="allowFullScreen" value="true" />'+
					(
						wl
						?''
						:'<param name="FlashVars" value="'+v+'" />'
					)+'<embed src="'+(wl?p+'?'+v:p)+'"  '+(wl?'':'flashvars="'+v+'"')+' ' +
							  'quality="high" ' +
							  'pluginspage="http://www.macromedia.com/go/getflashplayer" ' +
							  'type="application/x-shockwave-flash"  ' +
							  'width="'+w+'" ' +
							  'height="'+h+'"></embed>' +
				 '</object>';
	}
};